package com.durian.ui.adapter.multi;

import java.util.ArrayList;

/**
 * 说明：MultiTypeWrap
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-11-06 21:49
 * <p/>
 * 版本：version 1.0
 */
public class MultiTypeWrap {

    private String name;
    private int viewType;
    private ViewProvider provider;
    private ArrayList<MultiTypeWrap> multiWraps;

    public MultiTypeWrap(Class<?> clazz, ViewProvider provider) {
        this.name = clazz.getCanonicalName();
        this.viewType = -1;
        this.provider = provider;
        if (provider.isMultiType()){
            name = clazz.getCanonicalName();
            multiWraps = new ArrayList<>();
            ArrayList<ViewProvider> providers = provider.getAllMultiViewProviders();
            if (providers != null && !providers.isEmpty()){
                for (ViewProvider p:providers){
                    multiWraps.add(new MultiTypeWrap(p.getClass(),p));
                }
            }
        }
    }

    public int getViewType() {
        return viewType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public boolean isMultiType(){
        return provider.isMultiType();
    }

    public ArrayList<MultiTypeWrap> getMultiWraps(){
        return multiWraps;
    }

    public ViewProvider getProvider() {
        return provider;
    }

    public String getName() {
        return name;
    }

    public int getSize(){
        int size = 1;
        if (isMultiType()){
            size = multiWraps.size();
        }
        return size;
    }

    public void destroy(){
        if (provider.isMultiType()){
            if (multiWraps != null){
                for (MultiTypeWrap wrap:multiWraps){
                    wrap.provider.onDestroy();
                }
            }
        }else {
            provider.onDestroy();
        }
    }
}
